<?php
//include 'function.php';
//include 'DatabaseObject/Video.php';

class UtilityController extends Zend_Controller_Action
{
  private $db;
  private $content_table;
  private $cat_tags_table;
  public function init()
  {
    $db_obj = new myDb();
    $this->db = $db_obj->getDb();

    Zend_Db_Table::setDefaultAdapter($this->db);
    $this->content_table = new Application_Model_DbTable_Category();
    $this->cat_tags_table = new Application_Model_DbTable_Cattags();
  }

  public function indexAction()
  {
  }
  public function msgAction()
  {

  }
  public function getCollegesAction()
  {
    $pid = $this->getRequest()->getParam('pid');
    $sql = 'select coid, name from college where provinceID=\''.$pid.'\'';
    $this->db->setFetchMode(Zend_Db::FETCH_OBJ);
    $result = $this->db->fetchPairs($sql);

    foreach($result as $id=>$name)
      {
	$result[$id] = array('id'=>$id, 'name'=>urlencode($name));
      }
    $this->view->colleges = urldecode(json_encode($result));
  }

  public function getSchoolAction()
  {
    $cid = $this->getRequest()->getParam('cid');
    $sql = 'select scid, name from school where collegeID =\''.$cid.'\'';
    $this->db->setFetchMode(Zend_Db::FETCH_OBJ);
    $result = $this->db->fetchPairs($sql);

    foreach($result as $id=>$name)
      {
	$result[$id] = array('id'=>$id, 'name'=>urlencode($name));
      }
    $this->view->school = urldecode(json_encode($result));
  }
  public function getSubCatAction()
  {
    if ($this->getRequest()->isGet())
      {
	$cat_id = $this->getRequest()->getParam('cat_id');
	$where = $this->db->quoteInto('parent = ?', $cat_id);
	$cats = $this->content_table->fetchAll($where);

	$cat_array = array();
	foreach($cats as $cat)
	  {
	    //print $cat->id.': '.$cat->name.'<br/>';
	    $cat_array[$cat->id] = array('id'=>$cat->id, 'name'=>urlencode($cat->name));
	  }
	$this->view->cats = urldecode(json_encode($cat_array));
      }
  
  }
  
  public function getTagsAction()
  {
    if ($this->getRequest()->isGet())
      {
	$cat_id = $this->getRequest()->getParam('cat_id');
	$where = $this->db->quoteInto('cat_id = ?', $cat_id);
	$tags = $this->cat_tags_table->fetchAll($where);

	$tags_array = array();
	foreach ($tags as $tag)
	  {
	    $tags_array[$tag->id] = urlencode($tag->tag);
	  }
	$this->view->tags = urldecode(json_encode($tags_array));
      }
  }
  public function getCaptchaAction()
  {
    Zend_Session::start();
    $session = new Zend_Session_Namespace('account');

    $this->view->captcha_url = myCaptcha::create();
  }

  public function checkUsernameAction()
  {
    /* print "true"; */
    /* return ; */
    $username = $this->getRequest()->getParam('username');
    if (!isset($username) || empty($username))
      {
    	$this->view->hasone = "false";
    	return;
      }
    if (strpos($username, '@'))
      $field = 'email';
    else
      $field = 'username';
    
    $user = new Application_Model_DbTable_User();
    $res = $user->fetchAll("$field = '$username'");
    $this->view->hasone = count($res) > 0 ? 'false' : 'true';
    /* if () */
  }
};
